package com.example.first.adapters;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.first.R;
import com.example.first.activities.AddressEditActivity;
import com.example.first.modle.AddressBook;
import com.example.first.modle.ApiResponse;
import com.example.first.services.ApiService;
import com.example.first.utils.RetrofitClient;

import org.w3c.dom.Text;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class AddressAdapter extends BaseAdapter {


    private Context context;
    private ViewHolder viewHolder;
    private List<AddressBook> address;

    private AddressEditActivity activity;

    public AddressAdapter(Context context, List<AddressBook> address) {
        this.context = context;
        this.address = address;
    }

    @Override
    public int getCount() {
        return address.size();
    }

    @Override
    public Object getItem(int i) {
        return address.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.address_item, viewGroup, false);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        AddressBook addressBook = address.get(i);
        viewHolder.AddressName.setText(addressBook.getProvinceName()+addressBook.getCityName()+addressBook.getDistrictName()+addressBook.getDetail());
        viewHolder.personInfo.setText(addressBook.getConsignee()+"  "+addressBook.getPhone());
        viewHolder.checkBox.setChecked(addressBook.getIsDefault()==1);

        //设置"设为默认地址的点击事件"
        viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ApiService apiService= RetrofitClient.getApiService();
                Call<ApiResponse<String>> call=apiService.setDefaultAddress(addressBook);
                call.enqueue(new retrofit2.Callback<ApiResponse<String>>() {
                    @Override
                    public void onResponse(Call<ApiResponse<String>> call, retrofit2.Response<ApiResponse<String>> response) {
                        if(response.isSuccessful() && response.body()!=null){
                            ApiResponse<String> body=response.body();
                            if(body.getCode()==1){
                                //设置成功
                                ApiService apiService=RetrofitClient.getApiService();
                                Call<ApiResponse<List<AddressBook>>> call_1=apiService.getAddressList();
                                call_1.enqueue(new Callback<ApiResponse<List<AddressBook>>>() {
                                    @Override
                                    public void onResponse(Call<ApiResponse<List<AddressBook>>> call, Response<ApiResponse<List<AddressBook>>> response) {
                                        if(response.isSuccessful() && response.body()!=null){
                                            if(response.body().getCode()==1){
                                                //成功
                                                address=response.body().getData();
                                                setAddress(address);
                                            }
                                            else {
                                                Toast.makeText(context, response.body().getMsg(), Toast.LENGTH_SHORT).show();
                                            }
                                        }else {
                                            Toast.makeText(context, "请求失败", Toast.LENGTH_SHORT).show();
                                        }
                                    }

                                    @Override
                                    public void onFailure(Call<ApiResponse<List<AddressBook>>> call, Throwable t) {
                                        Toast.makeText(context, "请求失败", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }
                        }
                    }
                    @Override
                    public void onFailure(Call<ApiResponse<String>> call, Throwable t) {
                        //处理失败情况
                    }
                });
            }
        });
        //设置"编辑按钮的点击事件"
        viewHolder.edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到编辑界面

                Intent intent=new Intent(context, AddressEditActivity.class);
                intent.putExtra("address", address.get(i));
                context.startActivity(intent);
            }
        });

        return view;
    }


    public void setAddress(List<AddressBook> address) {
        this.address = address;
        notifyDataSetChanged();
    }


    static class ViewHolder {
        TextView AddressName;
        TextView personInfo;
        CheckBox checkBox;
        ImageButton edit;

        ViewHolder(View view) {
            AddressName = view.findViewById(R.id.address_text);
            personInfo=view.findViewById(R.id.address_info);
            checkBox=view.findViewById(R.id.address_checkbox);
            edit=view.findViewById(R.id.address_edit_button);
        }
    }
}
